import type { PositionType } from '@/hooks/usePosition';
import { defineStore } from 'pinia';
import { reactive } from 'vue';

interface Target {
  x: number;
  y: number;
}

export const useTargetStore = defineStore('target', () => {
  const targets: Target[] = reactive([]);

  function addTarget(target: Target) {
    targets.push(target);
  }

  function createTarget(position: { x: number; y: number }): Target {
    return {
      x: position.x,
      y: position.y,
    };
  }

  function isTarget(position: PositionType) {
    const index = targets.findIndex(
      (item) => item.x === position.x && item.y === position.y,
    );
    return index !== -1;
  }

  function clearAllTarget() {
    targets.splice(0, targets.length);
  }

  return { targets, addTarget, createTarget, isTarget, clearAllTarget };
});
